package com.cskaoyan.market.service.admin.impl;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.*;
import com.cskaoyan.market.service.admin.MarketUserManagerService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MarketUserManagerServiceImpl implements MarketUserManagerService {
    @Autowired
    MarketUserMapper userMapper;
    @Autowired
    MarketFootprintMapper footprintMapper;
    @Autowired
    MarketAddressMapper addressMapper;
    @Autowired
    MarketCollectMapper collectMapper;
    @Autowired
    MarketSearchHistoryMapper searchHistoryMapper;
    @Autowired
    MarketFeedbackMapper feedbackMapper;

    @Override
    public List<MarketAddress> getAddressList(Integer page, Integer limit, String name, String userId, String sort, String order) {

        MarketAddressExample addressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = addressExample.or().andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.valueOf(userId));
        }
        addressExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketAddress> marketAddresses = addressMapper.selectByExample(addressExample);

        return marketAddresses;
    }

    @Override
    public List<MarketCollect> getCollectList(Integer page, Integer limit, String userId, String valueId, String sort, String order) {

        MarketCollectExample collectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = collectExample.or().andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.valueOf(userId));
        }
        if (!StringUtils.isEmpty(valueId)) {
            criteria.andValueIdEqualTo(Integer.valueOf(valueId));
        }
        collectExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketCollect> marketCollects = collectMapper.selectByExample(collectExample);

        return marketCollects;
    }

    @Override
    public List<MarketFootprint> getFootprintList(Integer page, Integer limit, String userId, String goodsId, String sort, String order) {

        MarketFootprintExample footprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = footprintExample.or().andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.valueOf(userId));
        }
        if (!StringUtils.isEmpty(goodsId)) {
            criteria.andGoodsIdEqualTo(Integer.valueOf(goodsId));
        }
        footprintExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketFootprint> marketFootprints = footprintMapper.selectByExample(footprintExample);

        return marketFootprints;
    }

    @Override
    public List<MarketSearchHistory> getSearchHistoryList(Integer page, Integer limit, String keyword, String userId, String sort, String order) {

        MarketSearchHistoryExample historyExample = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = historyExample.or().andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.valueOf(userId));
        }
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        historyExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketSearchHistory> marketSearchHistories = searchHistoryMapper.selectByExample(historyExample);
        return marketSearchHistories;
    }

    @Override
    public List<MarketFeedback> getMarketFeedbackList(Integer page, Integer limit, String username, String id, String sort, String order) {

        MarketFeedbackExample feedbackExample = new MarketFeedbackExample();
        MarketFeedbackExample.Criteria criteria = feedbackExample.or().andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameEqualTo(username);
        }
        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        feedbackExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketFeedback> marketFeedbacks = feedbackMapper.selectByExample(feedbackExample);

        return marketFeedbacks;
    }

    @Override
    public List<MarketUser> getUserList(Integer page, Integer limit, String userId, String username, String mobile, String sort, String order) {
        MarketUserExample userExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = userExample.or().andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(userId)) {
            criteria.andIdEqualTo(Integer.valueOf(userId));
        }
        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (!StringUtils.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        userExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketUser> marketUsers = userMapper.selectByExample(userExample);

        return marketUsers;
    }

    @Override
    public MarketUser getUserById(Integer id) {
        MarketUser marketUser = userMapper.selectByPrimaryKey(id);
        return marketUser;
    }
}
